<?php

namespace App\Http\Controllers\Ajax;

use App\Common\Constant\Status;
use App\Services\ICookieService;
use App\Services\IMasterService;
use App\Services\ISessionService;
use App\Services\IWalletService;
use Illuminate\Http\Request;
use Validator;

class WalletController extends BaseController
{

    /**
     * 添加提现申请
     * @param Request $request
     * @param IWalletService $walletService
     */
    public function applyCash(Request $request,IWalletService $walletService)
    {
        if ($request->isMethod('post') && $request->has('amount')){
            $master_id = getCurrentUserid();
            $wallet = $walletService->getWalletByUserid($master_id);

            $validator = Validator::make($request->all(), [
                'amount' => "required|numeric|max:{$wallet->balance}",
            ],[
                'amount.max'=>'提现金额不能超过可提余额'
            ]);
            if ($validator->fails()){
                return $this->renderJson(Status::FAIL,'',$validator->errors()->first());
            }

            $ret = $walletService->applyCash($wallet,$request->input('amount'));
            if ($ret === Status::CASH_OK){
                return $this->renderJson(Status::SUCCESS);
            }else{
                return $this->renderJson($ret);
            }
        }
        return $this->renderJson(Status::ERROR);
    }
}
